package com.woan.api.services;

import java.util.List;

import net.sf.json.JSONObject;

import com.woan.api.idao.SysAdmininfoIDAO;
import com.woan.api.idao.SysPhotoIDAO;
import com.woan.api.model.SysPhoto;
import com.woan.api.util.ConfigUtil;
import com.woan.api.vo.VphotoList;

public class PhotoClientServices {
	private SysAdmininfoIDAO sysadmininfoiDAO;
	private SysPhotoIDAO sysphotoiDAO;	
	public SysPhotoIDAO getSysphotoiDAO() {
		return sysphotoiDAO;
	}
	public void setSysphotoiDAO(SysPhotoIDAO sysphotoiDAO) {
		this.sysphotoiDAO = sysphotoiDAO;
	}
	public SysAdmininfoIDAO getSysadmininfoiDAO() {
		return sysadmininfoiDAO;
	}
	public void setSysadmininfoiDAO(SysAdmininfoIDAO sysadmininfoiDAO) {
		this.sysadmininfoiDAO = sysadmininfoiDAO;
	}
	public List<VphotoList> getgroupphotos(String rid,Integer beginIndex,Integer endIndex){
		List<VphotoList> photo = null;
		photo = sysphotoiDAO.FindVPhotoByGroup(rid, beginIndex, endIndex);
		tranIp(photo);
		return photo;
	}
	public List<VphotoList> getmyphotoes(String uid,Integer beginIndex,Integer endIndex){
		List<VphotoList> photo = null;
		photo = sysphotoiDAO.FindVPhotoByUid(uid, beginIndex, endIndex);
		tranIp(photo);
		return photo;
	}
	public JSONObject editPhotos(String photoid,String title){
		String flag="0";
		JSONObject json=new JSONObject();
		flag = sysphotoiDAO.EditPhotos(photoid, title);
		if(flag.equals("0"))
		{
			json.accumulate("results", 0);
			json.accumulate("errMsg", "修改成功");
		}
		else
		{
			json.accumulate("results", 1);
			json.accumulate("errMsg", "修改失败");
		}
		return json;
	}
	public String save(SysPhoto photo){
		String flag="0";
		flag = sysphotoiDAO.save(photo);
		return flag;
	}
	
	/**
	 * 格式化内网ip
	 */
	private void tranIp(List<VphotoList> photos) {
		String neiwang_ip = ConfigUtil.get("neiwang_ip");
		if (photos.size()>0) {
			for (VphotoList photo : photos) {
				String url = photo.getUrl();
				if (url!=null&&url.length()>1) {
					String substring = url.substring(url.indexOf("/", 8));
					String newPathString=neiwang_ip+substring;
					photo.setUrl(newPathString);
				}
				String smurl =photo.getSmurl();
				if (smurl!=null&&smurl.length()>1) {
					String substring = smurl.substring(smurl.indexOf("/", 8));
					String newPathString=neiwang_ip+substring;
					photo.setSmurl(newPathString);
				}
			}
		}
	}
}
